﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QuerySOutBackNeedRepair : BasePage
    {
        int m_count = 0;
        string strCurrentReturnBillID = "";
     
        private DataTable CreateDT()
        {
            return BsOutrepair.CreateDT();
        }
       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                strCurrentReturnBillID = UIHelper.GetQueryStringValue("id");
                BindDDL();
                BindData(0);
            }
        }


        private void BindData(int pageIndex)
        {
           
            DataTable dt = 
                
                BsOutrepair.SelectOutNeedRepairDetail(strCurrentReturnBillID,txtBillID.Text,txtWorkOrderNo.Text,
                ddlSupplierID.SelectedValue, txtProcessID.Text, pageIndex, pcData.PageSize, out m_count );
                 pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
              // UIHelper.GroupRows(grdResult, "lblBillID", new int[] { 0, 1, 2, 3, 4, 5 });
 
            } else{
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }


        /// <summary>
        /// 绑定ddl
        /// </summary>
        private void BindDDL()
        {
            DataTable dt =MdSupplier.GetMdSupplierAll().Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.ddlSupplierID.DataSource = dt;
                this.ddlSupplierID.DataTextField = "SupplierName";
                this.ddlSupplierID.DataValueField = "SupplierID";
                this.ddlSupplierID.DataBind();
            }
            UIHelper.InsertFirstItem(ddlSupplierID);
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");
                rb.Attributes.Add("onclick", "judge(this)");
            }
        }


        private void AddPro()
        {
               GetAddPro();
        }

        string m_strResult = "";
        private void GetAddPro()
        {
             foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblDetail = gvr.FindControl("lblBillID") as Label;

                    if (lblDetail!=null && ((RadioButton)gvr.FindControl("rbSelect")).Checked)
                    {

                       
                      

                        string strSelectID = lblDetail.Text.Trim();
                          m_strResult = string.Format(@"{0}", strSelectID);
                      
                        break;
                    }
                    else
                    {
                        m_strResult = "";
                      
                    }
                }
            }
           
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            
             
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + m_strResult + "');CloseTopDialog();</script>");
            
        }

      

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }

        protected void rbBillSource_CheckedChanged(object sender, EventArgs e)
        {
            BindData(0);
        }
    }
}